﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Windows;
using System.Xml;
using CookComputing.XmlRpc;

namespace uchomeDesktop
{
    
    public partial class loginForm : Form
    {

        private Point mousePosition;

        public loginForm()
        {
            IuchomeAPI proxy = XmlRpcProxyGen.Create<IuchomeAPI>();
            proxy.Url = Properties.Settings.Default.uchApiUrl;
            //string ret = proxy.miniBlog_get(Properties.Settings.Default.auth, 1);

            InitializeComponent();
        }

        private void closeButton_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void minsizeButton_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
        }

        private void loginForm_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                //this.Cursor = Cursors.SizeAll;
                this.Top = MousePosition.Y - mousePosition.Y;
                this.Left = MousePosition.X - mousePosition.X;
            }
            else
            {
                this.Cursor = Cursors.Default;
            }
        }

        private void loginForm_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                //this.Cursor = Cursors.SizeAll;
                mousePosition.X = e.X;
                mousePosition.Y = e.Y;
            }
            else
            {
                this.Cursor = Cursors.Default;
            }
        }

        private void loginButton_Click(object sender, EventArgs e)
        {
            string uname = unameTextBox.Text;
            string pwd = pwdTextBox.Text;
            User user = new User();
            if (user.login(uname, pwd))
            {
                this.Hide();
                mainForm mainForm = new mainForm();
                mainForm.Show();
            }
            else
            {
                MessageBox.Show("登录失败，请检查用户密码");
            }
        }

        private void pwdTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                string uname = unameTextBox.Text;
                string pwd = pwdTextBox.Text;
                User user = new User();
                if (user.login(uname, pwd))
                {
                    this.Hide();
                    mainForm mainForm = new mainForm();
                    mainForm.Show();
                }
                else
                {
                    MessageBox.Show("登录失败，请检查用户密码");
                }
            }
        }

        private void unameTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                string uname = unameTextBox.Text;
                string pwd = pwdTextBox.Text;
                User user = new User();
                if (user.login(uname, pwd))
                {
                    this.Hide();
                    mainForm mainForm = new mainForm();
                    mainForm.Show();
                }
                else
                {
                    MessageBox.Show("登录失败，请检查用户密码");
                }
            }
        }

    }
}